package com.scap.authen.service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.BindingResult;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.support.RequestContextUtils;

import com.scap.authen.dao.SiteDao;
import com.scap.authen.dao.UserLoginDao;
import com.scap.authen.dao.UserMappingDao;
import com.scap.authen.entity.Application;
import com.scap.authen.entity.UserLogin;
import com.scap.authen.entity.UserMapping;
import com.scap.authen.util.Encryption;
import com.scap.authen.util.JDate;
import com.scap.authen.util.ValidateField;

@Service("userLoginService")
@Transactional
public class UserLoginServiceImpl implements UserLoginService {
	
	@Autowired
	private UserLoginDao voUserLoginDao;
	@Autowired
	private UserMappingDao voUserMappingDao;
	@Autowired
	private SiteDao voSiteDao;
	@Autowired
	private ApplicationContext context;

	@Override
	public List<Object> getUserLoginLastUpdate() {
		return voUserLoginDao.getUserLoginLastUpdate();
	}

	@Override
	public String insertUserLogin(UserLogin voUserLogin,String vaSiteAndRoleId,
			BindingResult result, HttpServletRequest request) {
		String vnResult = "";
		
		ValidateField voValidateField = new ValidateField();
		voValidateField.getObjectError(result);

		Encryption voEncryption = new Encryption();
		JDate voJdate = new JDate();
		if(!result.hasErrors()){
			if(vaSiteAndRoleId!=null && !vaSiteAndRoleId.equals("")){
				
				List<UserMapping> voUserMappingList = new ArrayList<UserMapping>();
				
				String[] arraySiteAndRoleIdSplit = vaSiteAndRoleId.split(",");
				
				for(String vaSiteAndRoleIdSplitTemp : arraySiteAndRoleIdSplit ){
					
					String[] arraySiteAndRoleIdSplitTemp = vaSiteAndRoleIdSplitTemp.split("|");
					List<String> list = new ArrayList<String>(Arrays.asList(arraySiteAndRoleIdSplitTemp));
					list.removeAll(Arrays.asList("|"));
					arraySiteAndRoleIdSplitTemp = list.toArray(arraySiteAndRoleIdSplitTemp);
					
					UserMapping voUserMapping = new UserMapping();
					voUserMapping.setVaSiteId(arraySiteAndRoleIdSplitTemp[0]);
					voUserMapping.setVaRoleId(arraySiteAndRoleIdSplitTemp[1]);
					voUserMapping.setVaUserId(voUserLogin.getVaUserId());
					voUserMapping.setVaInsertDate(voJdate.getDate());
					voUserMapping.setVaInsertTime(voJdate.getTime());
					voUserMapping.setVnInsertUserId(1);
					voUserMappingList.add(voUserMapping);
				}
				
				voUserMappingDao.insertUserMapping(voUserMappingList);

			}
			

			voUserLogin.setVaUserPassword(voEncryption.md5Encrypt(voEncryption.dataEncrypt(RandomStringUtils.randomAlphanumeric(8)+"_")));
			voUserLogin.setVaInsertDate(voJdate.getDate());
			voUserLogin.setVaInsertTime(voJdate.getTime());
			voUserLogin.setVnInsertUserId(1);
			
			if (voUserLoginDao.insertUserLogin(voUserLogin)) {
				vnResult = "success";
			} else {
				vnResult = "fail";
			}
			
		}
		
		return vnResult;
	}
	
	@Override
	public UserLogin getUserLoginByUserId(String vaUserId){
		return voUserLoginDao.getUserLoginByUserId(vaUserId);
		
	}

	@Override
	public String updateUserLogin(UserLogin voUserLogin,String vaSiteAndRoleId,
			BindingResult result, HttpServletRequest request) {
		String vnResult = "";
		
		Locale voLocale = RequestContextUtils.getLocale(request);
		ValidateField voValidateField = new ValidateField();
		voValidateField.getObjectError(result);
		
		if(!result.hasErrors()){
			
			JDate voJdate = new JDate();
			List<UserMapping> voUserMappingList = new ArrayList<UserMapping>();
			
			String[] arraySiteAndRoleIdSplit = vaSiteAndRoleId.split(",");
			
			for(String vaSiteAndRoleIdSplitTemp : arraySiteAndRoleIdSplit ){
				
				String[] arraySiteAndRoleIdSplitTemp = vaSiteAndRoleIdSplitTemp.split("|");
				List<String> list = new ArrayList<String>(Arrays.asList(arraySiteAndRoleIdSplitTemp));
				list.removeAll(Arrays.asList("|"));
				arraySiteAndRoleIdSplitTemp = list.toArray(arraySiteAndRoleIdSplitTemp);
				
				UserMapping voUserMapping = new UserMapping();
				voUserMapping.setVaSiteId(arraySiteAndRoleIdSplitTemp[0]);
				voUserMapping.setVaRoleId(arraySiteAndRoleIdSplitTemp[1]);
				voUserMapping.setVaUserId(voUserLogin.getVaUserId());
				voUserMapping.setVaInsertDate(voJdate.getDate());
				voUserMapping.setVaInsertTime(voJdate.getTime());
				voUserMapping.setVaUpdateDate(voJdate.getDate());
				voUserMapping.setVaUpdateTime(voJdate.getTime());
				voUserMapping.setVnInsertUserId(1);
				voUserMappingList.add(voUserMapping);
			}

			voUserLogin.setVaUpdateDate(voJdate.getDate());
			voUserLogin.setVaUpdateTime(voJdate.getTime());
			voUserLogin.setVnUpdateUserId(1);
			
			if(voUserMappingDao.deleteUserMapping(voUserMappingList.get(0))){
				if (voUserLoginDao.updateUserLogin(voUserLogin)) {
					voUserMappingDao.insertUserMapping(voUserMappingList);
					vnResult = "success";
				} else {
					vnResult = "fail";
				}
			}else {
				vnResult = "fail";
			}
			
			
		}
		
		return vnResult;
	}

	@Override
	public String deleteUserLogin(UserLogin voUserLogin,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Object> getListBasedOnSearchParameter(String vaUserId,String vaName,
			List<Object> userLoginList) {
		if (null != vaUserId && !vaUserId.equals("") && null != vaName && !vaName.equals("")) {
			List<Object> userLoginListForSearch = new ArrayList<Object>();
			vaUserId = vaUserId.toUpperCase();
			vaName=vaName.toUpperCase();
			for (int i=0; i<userLoginList.size(); i++) {
				
				UserLogin userLogin = (UserLogin) userLoginList.get(i);
				if (userLogin.getVaUserId().toUpperCase().indexOf(vaUserId)!= -1 && userLogin.getVaName().toUpperCase().indexOf(vaName)!= -1) {
					userLoginListForSearch.add(userLogin);					
				}
				
			}
			userLoginList = userLoginListForSearch;
			userLoginListForSearch = null;
		}
		return userLoginList;
	}
	
	@Override
	public List<HashMap<String, String>> getListBasedOnSearchParameterSiteAndRole(String searchParameter,
			List<HashMap<String, String>> voSiteAndRoleList) {
		if (null != searchParameter && !searchParameter.equals("")) {
			List<HashMap<String, String>> voSiteAndRoleListForSearch = new ArrayList<HashMap<String,String>>();
			searchParameter = searchParameter.toUpperCase();
			for (int i=0; i<voSiteAndRoleList.size(); i++) {
				
				HashMap<String, String> voSiteAndRole = voSiteAndRoleList.get(i);
				if (voSiteAndRole.get("0").toUpperCase().indexOf(searchParameter)!= -1 || voSiteAndRole.get("1").toUpperCase().indexOf(searchParameter)!= -1
						|| voSiteAndRole.get("2").toUpperCase().indexOf(searchParameter)!= -1 || voSiteAndRole.get("3").toUpperCase().indexOf(searchParameter)!= -1){
					voSiteAndRoleListForSearch.add(voSiteAndRole);					
				}
				
			}
			voSiteAndRoleList = voSiteAndRoleListForSearch;
			voSiteAndRoleListForSearch = null;
		}
		return voSiteAndRoleList;
	}

	@Override
	public List<Object> getAllUserLogin() {
		return voUserLoginDao.getAllUserLogin();
	}

}
